package com.dataaccess.dataaccess.web.web;

import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseController;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.HexUtils;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.DeviceAboatElectricService;
import com.dataaccess.dataaccess.web.service.DeviceAboatFirewaterService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 电气火灾-设备控制器
 */
@Action
@RequestMapping("electric")
public class DeviceAboatElectricController extends BaseController {

    @Resource
    public MyLogger logger;
    @Resource
    public DeviceAboatElectricService deviceAboatElectricService;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;
    @Resource
    UserProducer userProducer;
    @Resource
    DeviceAboatFirewaterService deviceAboatFirewaterService;


    /**
     * 订阅--调用相关接口
     *
     * @param
     * @return
     */
    @At(value = "device/getInfo", method = RequestMethod.POST)
    public RespEntity getInfo(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        return deviceAboatElectricService.getInfo(jsonParam);
    }

    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     *
     * @param
     * @return
     */
    @At(value = "device/deviceInfoChanged", method = RequestMethod.POST)
    public RespEntity deviceInfoChanged(@RequestBody(required = false) JSONObject jsonParam) {
        return deviceAboatElectricService.deviceInfoChanged(jsonParam);
    }

    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     *
     * @param
     * @return
     */
    @At(value = "device/deviceDataChanged", method = RequestMethod.POST)
    public RespEntity deviceDataChanged(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("eledeviceDataChanged" + "------" + jsonParam);
        String mqFlag = FirefightingiotKit.getStr("mqFlag", "mq");
        if (mqFlag.equals("1")) {
            JSONObject service = jsonParam.getJSONObject("service");
            String serviceType = service.getStr("serviceType");//服务id
            if (serviceType.equals("IotStringReturn")) {
                //和远智能
                return deviceAboatElectricService.deviceDataChanged(jsonParam);
            } else if (serviceType.equals("DataPackage")) {
                //盛善
                return deviceAboatElectricService.deviceDataChanged(jsonParam);
            } else {
                //水
                return deviceAboatFirewaterService.deviceDataChanged(jsonParam);
            }

        } else if (mqFlag.equals("3")) {
            try {
                JSONObject service = jsonParam.getJSONObject("service");
                String serviceType = service.getStr("serviceType");//服务id
                if (serviceType.equals("IotStringReturn")) {
                    //和远智能
                    jsonParam.put("deviceFlag", "electric");
                } else if (serviceType.equals("DataPackage")) {
                    //盛善
                    jsonParam.put("deviceFlag", "electric");
                } else {
                    //水
                    JSONObject data = service.getJSONObject("data");
                    jsonParam.put("deviceFlag", "firewater");
                }
                userProducer.produder(jsonParam.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 世成电器（电信）
     * @Date Created in 14:45 2022/5/25
     * @Param [jsonParam]
     */

    @At(value = "device/scdqElectricTelecom", method = RequestMethod.POST)
    public RespEntity scdqElectricTelecom(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("scdqElectricTelecom" + "------" + jsonParam);
        jsonParam.put("deviceFlag", "scdqElectricTelecom");
        String mqFlag = FirefightingiotKit.getStr("mqFlag", "mq");
        if (mqFlag.equals("1")) {
            return deviceAboatElectricService.scdqElectricTelecom(jsonParam);
        } else if (mqFlag.equals("3")) {
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 盛善（电信）
     * @Date Created in 14:45 2022/5/25
     * @Param [jsonParam]
     */

    @At(value = "device/shengshanElectricTelecom", method = RequestMethod.POST)
    public RespEntity shengshanElectricTelecom(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("shengshanElectricTelecom" + "------" + jsonParam);
        jsonParam.put("deviceFlag", "shengshanElectricTelecom");
        String mqFlag = FirefightingiotKit.getStr("mqFlag", "mq");
        if (mqFlag.equals("1")) {
            return deviceAboatElectricService.shengshanTelecom(jsonParam);
        } else if (mqFlag.equals("3")) {
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author XuLumin
     * @Description 海康电设备641(电信)
     * @Date Created in 08:45 2024/1/25
     * @Param [jsonParam]
     */

    @At(value = "device/hkElectricTelecom", method = RequestMethod.POST)
    public RespEntity hkElectricTelecom(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("hkElectricTelecom" + "------" + jsonParam);
        jsonParam.put("deviceFlag", "hkElectricTelecom");
        String mqFlag = FirefightingiotKit.getStr("mqFlag", "mq");
        if (mqFlag.equals("1")) {
            return deviceAboatElectricService.HK641(jsonParam);
        } else if (mqFlag.equals("3")) {
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 盛善（TCP）
     * @Date Created in 14:45 2022/5/25
     * @Param [jsonParam]
     */

    @At(value = "device/shengshanElectricTCP", method = RequestMethod.POST)
    public RespEntity shengshanElectricTCP(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("shengshanElectricTCP" + "------" + jsonParam);
        jsonParam.put("deviceFlag", "shengshanElectricTCP");
        String mqFlag = FirefightingiotKit.getStr("mqFlag", "mq");
        if (mqFlag.equals("1")) {
            return deviceAboatElectricService.shengshanTCP(jsonParam);
        } else if (mqFlag.equals("3")) {
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 海康智慧用电（TCP641）
     * @Date Created in 17:26 2023/11/13
     * @Param [jsonParam]
     */

    @At(value = "device/electricHik", method = RequestMethod.POST)
    public JSONObject electricHik(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("electricHik"+"------"+jsonParam);

        String code = jsonParam.getStr("code");
        //返回指令
        String returnCode = "";
        returnCode = code.substring(4,12) + HexUtils.currentTime() + "000000000000" + code.substring(24,36)+"000003";
        String crc = HexUtils.checkSum(returnCode);
        returnCode ="4040"+returnCode+crc+"2323";
        jsonParam.put("deviceFlag","electricHik");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            deviceAboatElectricService.electricHik(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        System.out.println("我是服务端，返回的数据为：" + returnCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",jsonParam.getStr("id"));
        jsonObject.put("data", returnCode);
        return jsonObject;
    }

}
